package com.blb.examsystem.service.Impl;

import com.blb.common.entity.PageBean;
import com.blb.common.entity.QuestionSet;
import com.blb.examsystem.dao.QuestionSetDao;
import com.blb.examsystem.service.QuestionSetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class QuestionSetServiceImpl implements QuestionSetService {
    @Autowired
    private QuestionSetDao questionSetDao;


    @Override
    public PageBean<QuestionSet> findPage(int current) {
        int dataCount = questionSetDao.selectCount();
        int pageCount = dataCount % PageBean.PAGE_SIZE == 0 ? dataCount / PageBean.PAGE_SIZE : dataCount / PageBean.PAGE_SIZE +1;
        List<QuestionSet> questionSets = questionSetDao.selectLimit((current - 1) * PageBean.PAGE_SIZE, PageBean.PAGE_SIZE);
        PageBean<QuestionSet> pageBean = new PageBean<>();
        pageBean.setCurrent(current);
        pageBean.setData(questionSets);
        pageBean.setDataCount(dataCount);
        pageBean.setPageCount(pageCount);
        return pageBean;
    }

    @Override
    public PageBean<QuestionSet> findPageByTitle(String title, int current) {
        int dataCount = questionSetDao.selectCountByTitle(title);
        int pageCount = dataCount % PageBean.PAGE_SIZE == 0 ? dataCount / PageBean.PAGE_SIZE : dataCount / PageBean.PAGE_SIZE +1;
        List<QuestionSet> questionSets = questionSetDao.selectLimitByTitle(title,(current - 1) * PageBean.PAGE_SIZE, PageBean.PAGE_SIZE);
        PageBean<QuestionSet> pageBean = new PageBean<>();
        pageBean.setCurrent(current);
        pageBean.setData(questionSets);
        pageBean.setDataCount(dataCount);
        pageBean.setPageCount(pageCount);
        return pageBean;
    }

    @Override
    public int addQuestionSet(QuestionSet questionSet) {
        return questionSetDao.insertQuestionSet(questionSet);
    }

    @Override
    public QuestionSet findById(int id) {
        List<QuestionSet> questionSets = questionSetDao.selectById(id);
        if (questionSets != null && questionSets.size()>0){
            return questionSets.get(0);
        }
        return null;
    }

    @Override
    public int modifyQuestionSet(QuestionSet questionSet) {
        return questionSetDao.updateQuestionSet(questionSet);
    }

    @Override
    public int removeQuestionSet(int id) {
        return questionSetDao.deleteQuestionSet(id);
    }
}
